package com.maya.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.maya.model.Question;
import com.maya.model.User;
import com.maya.service.UserService;




@Controller

public class SubmitQuestionController  {

	
	@Autowired
	private UserService userService;
		
	
	@RequestMapping(value = "/submitquestion",method = RequestMethod.POST)
    public String process(Question question,HttpSession session){
        
        User user = (User)session.getAttribute("currUser");   
        //检查身份
        if(null == user) {
            
            return "redirect:/viewallquestions";
        }
               
        
        if(null != question.getTitle()) {
        	        	
        	
            Question q = userService.ask(user,question.getTitle(),question.getBody());
            
            return "redirect:/viewquestion?qid=" + q.getId();
        }
        
        return "redirect:/viewallquestions";
    }
}
